package net.begincode.controller;

import net.begincode.common.bean.ResponsePage;
import net.begincode.handler.ConfigHandler;
import net.begincode.model.Config;
import net.begincode.param.ConfigParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by saber on 2017/9/25.
 */
@RequestMapping("/conf/config")
@Controller
public class ConfigController {

    @Resource
    private ConfigHandler configHandler;

    @ResponseBody
    @RequestMapping(value = "/page" ,method = RequestMethod.GET)
    public Object configPage(@Param("serverId") Integer serverId, @Param("page") Integer page,@Param("limit") Integer limit){
        Integer serverConfigStopCount = configHandler.countServerConfig(serverId);
        List<Config> serverConfig = configHandler.listServerConfig(serverId,page,limit);
        return new ResponsePage("0",serverConfigStopCount,serverConfig,"");
    }

    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object listConfig(Integer serverId, Integer currentNum, Integer eachSize){
        return configHandler.listServerConfig(serverId, currentNum, eachSize);
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Object deleteConfig(Integer configId){
        return configHandler.deleteConfig(configId);
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String updateConfig(ConfigParam configParam ,Integer configId){
        Config config = configParam.getConfig();
        configHandler.updateConfig(config ,configId);
        return "config";
    }

    @RequestMapping(value = "/insert", method = RequestMethod.GET)
    public String insertConfig(ConfigParam configParam){
        Config config = configParam.getConfig();
        configHandler.insertConfig(config);
        return "config";
    }
}
